package com.bookRecommend.controller;

import com.bookRecommend.commons.Constant;
import com.bookRecommend.commons.ResponseUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.ServletException;
import javax.servlet.http.Part;
import java.io.IOException;
import java.util.Collection;

@RestController
@RequestMapping("/upload")
public class UploadController {

    @RequestMapping("")
    public ResponseUtils upload(MultipartHttpServletRequest request) {
        try {
            Collection<Part> parts = request.getParts();
            for (Part part : parts) {
                part.write(Constant.UPLOAD_DIR + System.currentTimeMillis() + ".jpg");
            }
            return ResponseUtils.success();
        } catch (IOException | ServletException e) {
            return ResponseUtils.failure("上传失败！");
        }

    }
}
